package com.kzw.oa.byxt.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kzw.core.orm.Page;
import com.kzw.core.orm.PageRequest;
import com.kzw.core.orm.StringPropertyFilter;
import com.kzw.core.util.BeanUtil;
import com.kzw.core.util.ExtjsUtil;
import com.kzw.core.util.StringUtil;
import com.kzw.core.web.action.BaseAction;
import com.kzw.oa.byxt.model.CheckMember;
import com.kzw.oa.byxt.service.CheckMemberService;
import com.kzw.oa.byxt.service.SubjectService;
import com.kzw.oa.model.system.AppUser;
import com.kzw.oa.util.JSON;
import com.kzw.oa.util.JSON2;

@Controller
@Scope("prototype")
public class CheckMemberAction extends BaseAction {
	
	@Autowired
	private CheckMemberService checkMemberService;	
	@Autowired
	private SubjectService subjectService;
	
	private CheckMember checkMember;
	private Long oid;

	/**
	 * 显示列表
	 */
	public String list() {
		HttpServletRequest request = getRequest();

		PageRequest pageRequest = ExtjsUtil.getPageReq(request);
		List<StringPropertyFilter> filters = ExtjsUtil.getFilters(request);
	
		if (ExtjsUtil.isExport(request)) { // 导出报表
			List<CheckMember> resultList = checkMemberService.search(filters);
			request.setAttribute("__exportList", resultList);
			return SUCCESS;
		}

		Page<CheckMember> page = checkMemberService.search2(pageRequest, filters);

		jsonString = new JSON2(page).with(AppUser.class, "fullname").toJson();
		return SUCCESS;
	}

	/**
	 * 批量删除
	 */
	public String multiDel() {
		String[] ids = getRequest().getParameterValues("ids");
		checkMemberService.removeCascade(ids);
		jsonString = "{success:true}";
		return SUCCESS;
	}

	/**
	 * 显示详细信息
	 * 
	 * @return
	 */
	public String get() {
		CheckMember checkMember = checkMemberService.get(oid);
		setJsonString(new JSON(checkMember).buildWithFilters(2));
		return SUCCESS;
	}

	/**
	 * 添加及保存操作
	 */
	public String save() {
		if (checkMember.getOid() == null) {
			checkMemberService.saveOrUpdate(checkMember);
		} else {
			CheckMember orgCheckMember = checkMemberService.get(checkMember.getOid());
			try {
				BeanUtil.copyNotNullProperties(orgCheckMember, checkMember);
				checkMemberService.saveOrUpdate(orgCheckMember);
			} catch (Exception ex) {
				logger.error(ex.getMessage());
			}
		}
		setJsonString("{success:true}");
		return SUCCESS;
	}

	/**
	 * 关联下拉
	 */
	public String combo() {
		String hql = "from CheckMember";
		String gid  = getRequest().getParameter("gid");
		if(StringUtils.isNotBlank(gid)) {
			hql += " where grade.oid="+gid;
		}
		List<CheckMember> list = checkMemberService.findByHQL(hql);
		jsonString = new JSON2(list).with(CheckMember.class, "oid", "name").toJson();
		return SUCCESS;
	}

	/**
	 * 导出选中记录数
	 * */
	public String exportSel() {
		HttpServletRequest request = getRequest();
		if (ExtjsUtil.isExport(request)) {
			String sids = request.getParameter("sids");
			List<CheckMember> result = checkMemberService.getByIds(StringUtil.idStrToArr(
					sids, Long.class));
			request.setAttribute("__exportList", result);
		}

		return SUCCESS;
	}	
	
	/**
	 * 增加审题教师
	 * */
	public String addTeachers() {
		String userIds = getRequest().getParameter("userIds");
		if (StringUtils.isNotEmpty(userIds)) {
			String[] uIds = userIds.split(",");
			for (String id : uIds) {
				if(StringUtils.isNotBlank(id)) {
					String hql = "from CheckMember x where x.member.userId=?";
					CheckMember checkMember = checkMemberService.findUniqueByHQL(hql, Long.parseLong(id));
					if(checkMember == null) {
						checkMember = new CheckMember();
						checkMember.setMember(new AppUser(Long.parseLong(id)));
						checkMemberService.saveOrUpdate(checkMember);
					}
				}				
			}
		}
		return SUCCESS;
	}
	

	/**
	 * 增加题目
	 * */
	public String addSubject() {
		
		CheckMember member = checkMemberService.get(oid);
		String subjectIds = getRequest().getParameter("subjectIds");
		String subjectNames = getRequest().getParameter("subjectNames");
		
//		String newSubjectIds = "";
//		String newSubjectNames = "";
//		
//		String[] oids = subjectIds.split(",");
//		for(String oid : oids) {
//			if(StringUtils.isBlank(oid)) {
//				break;
//			}
//			Subject subject = subjectService.get(Long.parseLong(oid));
//			// 只有为待审核状态的题目才能指派给老师审核
//			if(subject.getStatus() == 2) {
//				newSubjectIds += oid + ","; //必须是英文逗号
//				newSubjectNames += subject.getName() + "，";
//			}
//		}
//		if(!newSubjectNames.equals("")) {
//			newSubjectNames = newSubjectNames.substring(0, newSubjectNames.length() -1);
//			newSubjectIds = newSubjectIds.substring(0, newSubjectIds.length()-1);
//		}
//		
//		member.setSubjectIds(newSubjectIds);
//		member.setSubjectNames(newSubjectNames);
		
		member.setSubjectIds(subjectIds);
		member.setSubjectNames(subjectNames);
		checkMemberService.saveOrUpdate(member, subjectIds);
		
		return SUCCESS;
	}
	
	
	
	public Long getOid() {
		return oid;
	}

	public void setOid(Long oid) {
		this.oid = oid;
	}

	public CheckMember getCheckMember() {
		return checkMember;
	}

	public void setCheckMember(CheckMember checkMember) {
		this.checkMember = checkMember;
	}

}
